package com.andatsoft.laisim.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.entity.AlbumItem;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Animator;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.Utils;

public class LibraryGridAlbumAdapter extends BaseAdapter {

	private List<AlbumItem> mListAlbum;
	private LayoutInflater mInflater;
	private int mSelectedItem = -1;
	private Theme mTheme;

	public LibraryGridAlbumAdapter(Context context, List<AlbumItem> items) {
		mListAlbum = items;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTheme = ThemeManager.getSelectedTheme();
	}

	@SuppressLint("NewApi")
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vHolder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.library_grid_album_item,
					parent, false);
			vHolder = new ViewHolder();
			// set value into textview
			TextView textView = (TextView) convertView
					.findViewById(R.id.tv_library_grid_album_name);
			vHolder.tvName = textView;
			textView = (TextView) convertView
					.findViewById(R.id.tv_library_grid_album_artist);
			vHolder.tvArtist = textView;
			vHolder.tvLength = (TextView) convertView
					.findViewById(R.id.tv_library_grid_album_length);
			ImageView img = (ImageView) convertView
					.findViewById(R.id.img_library_grid_album);
			vHolder.imgIcon = img;
			convertView.setTag(vHolder);

		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}

		// Apply theme
		if (position == this.mSelectedItem) {

			vHolder.tvName.setTextSize(mTheme.getMPLSelectedTitleSize());
			vHolder.tvName.setTextColor(mTheme.getMPLSelectedTitleColor());
			vHolder.tvArtist.setTextColor(mTheme.getMPLSelectedArtistColor());
			vHolder.tvArtist.setTextSize(mTheme.getMPLSelectedArtistSize());
		} else {

			vHolder.tvName.setTextSize(mTheme.getMPLNormalTitleSize());
			vHolder.tvName.setTextColor(mTheme.getMPLNormalTitleColor());
			vHolder.tvArtist.setTextColor(mTheme.getMPLNormalArtistColor());
			vHolder.tvArtist.setTextSize(mTheme.getMPLNormalArtistSize());
		}
		AlbumItem ai = mListAlbum.get(position);
		vHolder.tvName.setText(ai.name);
		vHolder.tvArtist.setText(ai.artist);
		vHolder.tvLength.setText(ai.length);

		if (ThemeManager.getSelectedSkin() != null) {
			if (ThemeManager.getSelectedSkin().getLibScreenAlbum() != null
					&& !((BitmapDrawable) ThemeManager.getSelectedSkin()
							.getLibScreenAlbum()).getBitmap().isRecycled())

				vHolder.imgIcon.setImageDrawable(ThemeManager.getSelectedSkin()
						.getLibScreenAlbum());
		}
		if (ai.bitmapArt != null) {
			vHolder.imgIcon.setImageBitmap(ai.bitmapArt);
			Animator.applyAlphaAnimation(vHolder.imgIcon, 1000, null, 0.0f, 1.0f);
		} else {
			if (Utils.androidOs == Common.ANDROID_OS_GINGERBREAD) {
				new AlbumArtGetter(vHolder.imgIcon).execute(ai.albumArtPath);
			} else {
				new AlbumArtGetter(vHolder.imgIcon).executeOnExecutor(
						AsyncTask.THREAD_POOL_EXECUTOR, ai.albumArtPath);
			}
		}
		return convertView;
	}

	@Override
	public int getCount() {
		return mListAlbum.size();
	}

	public void setData(List<AlbumItem> items) {
		this.mListAlbum = items;
		// notifyDataSetChanged();
	}

	public void setSelectedItem(int pos) {
		if (pos > mListAlbum.size() - 1 || pos < 0)
			pos = -1;
		this.mSelectedItem = pos;
	}

	class ViewHolder {
		ImageView imgIcon;
		TextView tvName;
		TextView tvArtist;
		TextView tvLength;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	class AlbumArtGetter extends AsyncTask<String, Void, Bitmap> {
		private ImageView imageView;

		public AlbumArtGetter(ImageView imgV) {
			this.imageView = imgV;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			String path = params[0];
			Bitmap bm = BitmapFactory.decodeFile(path);
			if (bm == null)
				return null;
			// return getRoundedShape(Bitmap
			// .createScaledBitmap(bm, 200, 200, true));
			return bm;
		}

		// private Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
		// // TODO Auto-generated method stub
		// int targetWidth = 200;
		// int targetHeight = 200;
		// Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
		// targetHeight, Bitmap.Config.ARGB_8888);
		//
		// Canvas canvas = new Canvas(targetBitmap);
		// Path path = new Path();
		// path.addCircle(
		// ((float) targetWidth - 1) / 2,
		// ((float) targetHeight - 1) / 2,
		// (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
		// Path.Direction.CCW);
		//
		// canvas.clipPath(path);
		// Paint bmPaint = new Paint();
		// bmPaint.setAntiAlias(true);
		// bmPaint.setFilterBitmap(true);
		// canvas.drawBitmap(scaleBitmapImage,
		// new Rect(0, 0, scaleBitmapImage.getWidth(),
		// scaleBitmapImage.getHeight()), new Rect(0, 0,
		// targetWidth, targetHeight), bmPaint);
		// return targetBitmap;
		// }

		@Override
		protected void onPostExecute(Bitmap result) {
			if (result != null) {
				Animator.applyAlphaAnimation(imageView, 1000, null, 0.0f, 1.0f);
				imageView.setImageBitmap(result);
			}
		}

	}

}
